package com.example.ui.learn;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.huashangapp.R;
import com.example.net.AnalyzingJson;
import com.example.net.NetRequest;
import com.example.ui.learn.util.WheelMain;

import com.example.util.ArrayWheelAdapter1;
import com.example.util.OnWheelChangedListener;
import com.example.util.WheelView;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class InviteActivity extends Activity {
	private EditText ia_etcompany, ia_etaddress, ia_etname, ia_etmobilephone,
			ia_etemail, ia_etphone;
	private String company, address, name, mobilephone, email, phone;
	private EditText ia_etpeoplenums;
	private TextView ia_days;
	private String days = "", peoplenums, teacher = "", startdate = "",
			enddate = "", theme;
	private TextView ia_tvteacher, ia_tvstartdate, ia_tvenddate, ia_tvtheme;
	private RelativeLayout ia_rlstartdate, ia_rlenddate, ia_rldays;
	private LinearLayout ia_llbottom;
	private LinearLayout ia_ll;
	private String time;
	private View timePicker1;
	private WheelMain wheelMain;//选择器的主类
	private Button ia_ok, ia_daysok;
	private int num;
	// top
	private Button btnleft, btnright;
	private TextView tvtitle;
	private WheelView wvdays;//时间选择器 
	private String data;
	private LinearLayout ia_llselectdays;
	private List<String> l_days;
	private List<String> list = new ArrayList<String>();;
	private String result;
	private String tid,title;//教师id
	
	AlertDialog infoad;
	AlertDialog.Builder bulider;
    
    DatePicker dp;
    Button okfordp;
    
   
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_invite);
		Intent i = getIntent();
		tid = i.getStringExtra("tid");
		title=i.getStringExtra("title");
		teacher=i.getStringExtra("name");
	
		init();
		ia_tvtheme.setText(title);
		ia_tvteacher.setText(teacher);
	}

	private Handler h = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
		
			case 2:
				infoad.dismiss();
				if (result.equals("true")) {
					Toast.makeText(InviteActivity.this, "提交成功",
							Toast.LENGTH_LONG).show();
					finish();
				} else {
					Toast.makeText(InviteActivity.this, "提交失败，请重试",
							Toast.LENGTH_LONG).show();
				}

				break;
			default:
				break;
			}

		};

	};

	Runnable sumbit_run = new Runnable() {

		@Override
		public void run() {			
			
			bulider = new AlertDialog.Builder(InviteActivity.this);
			bulider.setView(View.inflate(InviteActivity.this, R.layout.onloading, null))
					.create();
			infoad = bulider.show();
			data = NetRequest.postRequestinvite(list);
			result = AnalyzingJson.return_iaresult(data);
			h.sendEmptyMessage(2);
		}
	};

	private void init() {
		ia_llselectdays = (LinearLayout) findViewById(R.id.ai_selectdays);
		ia_rldays = (RelativeLayout) findViewById(R.id.ai_rldays);
		// top
		btnleft = (Button) findViewById(R.id.layout_title_left_btn);
		btnright = (Button) findViewById(R.id.layout_title_right_btn);
		tvtitle = (TextView) findViewById(R.id.layout_title_middle_text);
		btnleft.setText("返回");
		tvtitle.setText("讲师邀约");
		btnright.setText("提交");
		//
		ia_daysok = (Button) findViewById(R.id.ar4_btn_selectdays);
		ia_days = (TextView) findViewById(R.id.ai_tvdays);
		ia_etpeoplenums = (EditText) findViewById(R.id.ai_etpeoplenums);

		ia_etcompany = (EditText) findViewById(R.id.ai_etcompany);
		ia_etaddress = (EditText) findViewById(R.id.ai_etaddress);
		ia_etname = (EditText) findViewById(R.id.ai_etname);
		ia_etmobilephone = (EditText) findViewById(R.id.ai_etmobile);
		ia_etemail = (EditText) findViewById(R.id.ai_etemail);
		ia_etphone = (EditText) findViewById(R.id.ai_etphone);
		//
		ia_tvteacher = (TextView) findViewById(R.id.ai_tvteacher);
		
		ia_tvstartdate = (TextView) findViewById(R.id.ai_tvstartdate);
		ia_tvenddate = (TextView) findViewById(R.id.ai_tvenddate);
		ia_tvtheme = (TextView) findViewById(R.id.ai_tvtheme);
		ia_rlstartdate = (RelativeLayout) findViewById(R.id.ai_rlstartdate);
		ia_rlenddate = (RelativeLayout) findViewById(R.id.ai_rlenddate);
		ia_llbottom = (LinearLayout) findViewById(R.id.ai_llbottom);
		ia_ll = (LinearLayout) findViewById(R.id.ai_ll);
		ia_tvstartdate = (TextView) findViewById(R.id.ai_tvstartdate);
		ia_tvenddate = (TextView) findViewById(R.id.ai_tvenddate);
		//
		timePicker1 = findViewById(R.id.timepicker1);
		wvdays = (WheelView) findViewById(R.id.days);
		initDays();
		wheelMain = new WheelMain(timePicker1);
		wheelMain.initDateTimePicker();
		ia_ok = (Button) findViewById(R.id.ai_btnok);
		ia_ok.setOnClickListener(ia_ocl);
		ia_ll.setOnClickListener(ia_ocl);
		ia_rlstartdate.setOnClickListener(ia_ocl);
		ia_rlenddate.setOnClickListener(ia_ocl);
		ia_rldays.setOnClickListener(ia_ocl);
		ia_daysok.setOnClickListener(ia_ocl);
		btnright.setOnClickListener(ia_ocl);
		btnleft.setOnClickListener(ia_ocl);
	}

	private class gListener implements OnWheelChangedListener {
		@Override
		public void onChanged(WheelView wheel, int oldValue, int newValue) {
			days = l_days.get(newValue);
		}
	}

	private OnClickListener ia_ocl = new OnClickListener() {

		@Override
		public void onClick(View v) {
		
			switch (v.getId()) {

			case R.id.ai_rldays:
				ia_llselectdays.setVisibility(View.VISIBLE);
				break;
			case R.id.layout_title_left_btn:
				finish();
				break;
			case R.id.layout_title_right_btn:
				
				peoplenums = ia_etpeoplenums.getText().toString();
				company = ia_etcompany.getText().toString();
				address = ia_etaddress.getText().toString();
				name = ia_etname.getText().toString();
				mobilephone = ia_etmobilephone.getText().toString();
				email = ia_etemail.getText().toString();
				phone = ia_etphone.getText().toString();			
				list.add(theme);
				list.add(tid);
				list.add(teacher);
				list.add(days);
				list.add(startdate+" "+enddate);				
				list.add(peoplenums);				
				list.add(company);
				list.add(address);
				list.add(name);
				list.add(email);
				list.add(mobilephone);				
				list.add(phone);
				Boolean isNull = null;
				for (int i = 0; i < list.size(); i++) {
					String s = list.get(i);
					if ("".equals(s)) {
						Toast.makeText(InviteActivity.this, "请完整填写资料",
								Toast.LENGTH_SHORT).show();
						isNull = true;
						break;
					}
					isNull = false;
				}
				if (isNull == false) {
					new Thread(sumbit_run).start();
				}

				break;
			case R.id.ar4_btn_selectdays:
				ia_llselectdays.setVisibility(View.GONE);
				if (days.equals("")) {
					days = "0.5";
				}
				ia_days.setText(days);
				break;
			case R.id.ai_ll:
				ia_llbottom.setVisibility(View.GONE);
				ia_llselectdays.setVisibility(View.GONE);
				break;
			case R.id.ai_btnok:
				ia_llbottom.setVisibility(View.GONE);
				time = wheelMain.getTime();
				switch (num) {
				case 1:
					startdate = time;
					ia_tvstartdate.setText(time);
					break;
				case 2:
					enddate = time;
					ia_tvenddate.setText(time);
					break;

				default:
					break;
				}
				break;
			case R.id.ai_rlstartdate:
				
				initDatePicker();
				infoad = bulider.show();
				
				num = 1;
				break;
			case R.id.ai_rlenddate:
				initDatePicker();
				infoad = bulider.show();
				num = 2;
				break;
			default:
				break;
			}
		}
	};
	private void initDatePicker(){
		View v=View.inflate(InviteActivity.this, R.layout.selectdate, null);
		bulider = new AlertDialog.Builder(InviteActivity.this);
		bulider.setView(v).create();
		dp=(DatePicker) v.findViewById(R.id.dp);
		okfordp=(Button)v.findViewById(R.id.okfordp);
		okfordp.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				time=dp.getYear()+"-"+dp.getMonth()+"-"+dp.getDayOfMonth();
				switch (num) {
				case 1:
					ia_tvstartdate.setText(time);
					break;
				case 2:
					ia_tvenddate.setText(time);
					break;
				default:
					break;
				}
				infoad.dismiss();
				
			}
		});
		  Time t = new Time("GMT+8");    
	        t.setToNow();   	      		
		dp.init(t.year, t.month, t.monthDay, new OnDateChangedListener() {
			
			@Override
			public void onDateChanged(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				
				
			}
		});
	}

	private void initDays() {
		l_days = new ArrayList<String>();
		l_days.add("0.5");
		l_days.add("1");
		l_days.add("1.5");
		l_days.add("2");
		l_days.add("2.5");
		l_days.add("3");
		l_days.add("3.5");
		l_days.add("4");
		l_days.add("4.5");
		l_days.add("5");
		wvdays.setAdapter(new ArrayWheelAdapter1(l_days));
		wvdays.addChangingListener(new gListener());
	}
}
